package cn.chair.controller.admin;

import cn.chair.dao.SeatDao;
import cn.chair.entity.Seat;
import cn.chair.model.admin.SeatResp;
import cn.chair.service.DeviceService;
import life.innovation.builder.SqlBuilder;
import life.innovation.exception.DataReadException;
import life.innovation.exception.DataWriteException;
import life.innovation.exception.FailedDependencyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping(value="seat")
@RestController
public class SeatController {

    @Autowired
    @Lazy
    private SeatDao seatDao;

    @Autowired
    @Lazy
    private DeviceService deviceService;

    @Autowired
    @Lazy
    private JdbcTemplate jdbcTemplate;

    /**
     * 解除座位绑定
     * @param merchantId
     * @param chairId
     * @return
     * @throws DataWriteException
     */
    @DeleteMapping(value="deleteSeat")
    public Map<String,Object> deleteSeat(@RequestParam int merchantId, @RequestParam int chairId,@RequestParam String seatNo) throws DataWriteException, FailedDependencyException {
        Map<String,Object> map = new HashMap<>();

        Seat seat = seatDao.findOne(SqlBuilder.init().where("merchant_id",merchantId).where("chair_id",chairId).where("seat_no",seatNo));
        if(seat == null){
          throw new FailedDependencyException("该座位未有设备绑定");
        }

        String sql = "delete from seat where chair_id = ? and merchant_id = ?";
        jdbcTemplate.update(sql,chairId,merchantId);
//        seatDao.delete(chairId);
        map.put("success",true);
        return map;
    }

    /**
     * 绑定座位
     * @param merchantId
     * @param chairId
     * @param seatNo
     */
    @PostMapping(value="addSeat")
    @Transactional
    public Map<String,Object> add(@RequestParam int merchantId,@RequestParam int chairId,@RequestParam String seatNo) throws FailedDependencyException, DataWriteException {

        Seat seat = seatDao.findOne(SqlBuilder.init().where("merchant_id",merchantId).where("seat_no",seatNo));
        if(seat != null){
            throw  new FailedDependencyException("该座位已有设备绑定");
        }

        Seat s = seatDao.findOne(SqlBuilder.init().where("chair_id",chairId));
        if(s != null){
            s.setSeatNo(seatNo);
            s.setSeatName(seatNo.split(",")[0]+"排"+seatNo.split(",")[1]+"座");
            seatDao.update(s);
        }else{
            Seat ss = new Seat();
            ss.setMerchantId(merchantId);
            ss.setChairId(chairId);
            ss.setSeatNo(seatNo);
            ss.setSeatName(seatNo.split(",")[0]+"排"+seatNo.split(",")[1]+"座");
            seatDao.insert(ss);
        }

        Map<String,Object> map = new HashMap<>();
        map.put("success",true);
        return map;

    }

    /**
     *查询影院所有绑定设备
     * @param merchantId
     * @return
     */
    @GetMapping(value="getSeat")
    public List<SeatResp> getSeat(@RequestParam int merchantId) throws DataReadException {
        List<SeatResp> seats = seatDao.getSeats(merchantId);
        for(SeatResp resp:seats){
            boolean flag = deviceService.getDeviceisRun(String.valueOf(resp.getChairId()));
            if(flag){
                resp.setChairStatus(1);
            }
        }
        return seats;
    }
}
